/**
 *  Copyright (c) 2017-2022 Rushang Co.Ltd All Rights Reserved.
 */
package com.rshang.rsdp.exception;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.rshang.rsdp.bean.Response;
import com.rshang.rsdp.common.status.StatusCode;
import com.rshang.rsdp.util.i18n.I18nMessage;


/**
 * 功能简介：全局异常处理器<br/>
 * @author zhangjun
 * @create 2017年7月31日下午4:43:29
 * 
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);		
	public static final String DEFAULT_ERROR_VIEW = "error";
	
	@Autowired
	I18nMessage i18n;
	
	/**
	 * 系统异常处理
	 * @param request   HttpServletRequest对象
	 * @param e			异常对象
	 * @return
	 * 		Response
	 */
	@ExceptionHandler(value=RsdpSystemException.class)
	@ResponseBody
	public Response systemException(HttpServletRequest request,RsdpSystemException e){
		logger.error("RsdpSystemException:{} - {} - {}",request.getRequestURL(),e.getCode(),e.getErrMsg());
		return new Response(e.getCode(),e.getErrMsg());
	}
	
	
    /**
     * 默认异常错误处理
	 * @param request   HttpServletRequest对象
	 * @param e			异常对象
     * @return	ModelAndView
     * @throws Exception
     */
    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception {
    		logger.error("page error:{}",request.getRequestURL(),e);
        if(request.getHeader("x-requested-with")!=null)//ajax请求
        {
        		MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
        		jsonView.setExtractValueFromSingleKeyModel(true);
        		ModelAndView mav = new ModelAndView(jsonView);
        		mav.addObject(new Response(StatusCode.SERVER_ERROR.getCode(),e.getMessage()));
        		return mav;
        }
        else
        {
        		ModelAndView mav = new ModelAndView(DEFAULT_ERROR_VIEW);
            mav.addObject("exception", e.getMessage());
            return mav;
        }
    }
    
    /**
     * 404错误异常处理方法
     * @param request	HttpServletRequest对象
     * @param e			异常对象
     * @return
     * 		Response
     */
    @ExceptionHandler(value=NoHandlerFoundException.class)
	@ResponseBody
	public Response noHandlerException(HttpServletRequest request,NoHandlerFoundException e){
		logger.error("NoHandlerFoundException:{} - {}",request.getRequestURL(),e.getMessage());
		return new Response(StatusCode.REQUEST_NOTFOUND.getCode(),i18n.getMessage(StatusCode.REQUEST_NOTFOUND.getKey()));
	}
}

